perm filename BNCH9.PL[PLC,LSP] blob
sn#763188 filedate 1984-08-03 generic text, type T, neo UTF8
% [15] Differentiation
% This program is the same as that written in Lisp.
:- public d/3, diff1/1, diff2/1.
:- public q151/1, q152/1.
/*
To optimize the compiled code, add the next declarations:
:- mode d(+,+,-), simp←plus(+,+,-), simp←diff(+,+,-).
:- mode simp←times(+,+,-), simp←expt(+,+,-).
:- mode diff1(-), diff2(-).
:- mode q151(-), q152(-).
:- fastcode.
:- compactcode.
*/
:-op(300,xfy,↑).
d(U+V,X,DY) :- !, d(U,X,DU), d(V,X,DV), simp←plus(DU,DV,DY).
d(U-V,X,DY) :- !, d(U,X,DU), d(V,X,DV), simp←diff(DU,DV,DY).
d(U*V,X,DY) :- !, d(U,X,DU), d(V,X,DV),
simp←time(DU,V,D1), simp←time(U,DV,D2), simp←plus(D1,D2,DY).
d(U↑0,X,0) :- !.
d(U↑1,X,DY) :- !, d(U,X,DY).
d(U↑N,X,DY) :- !, N1 is N-1, simp←expt(U,N1,Y), d(U*Y,X,DY).
d(X,X,1) :-!.
d(C,X,0) :- atomic(C), C \== 0.
simp←plus(X,Y,Z) :- integer(X), integer(Y), !, Z is X+Y.
simp←plus(0,Y,Y) :- !.
simp←plus(X,0,X) :- !.
simp←plus(X,Y,X+Y).
simp←diff(X,Y,Z) :- integer(X), integer(Y), !, Z is X-Y.
simp←diff(0,Y,-1*Y) :- !.
simp←diff(X,0,X) :- !.
simp←diff(X,Y,X+V) :- integer(Y), !, V is -Y.
simp←diff(X,Y,X-Y).
simp←time(X,Y,Z) :- integer(X), integer(Y), !, Z is X*Y.
simp←time(←,0,0) :- !.
simp←time(0,←,0) :- !.
simp←time(X,1,X) :- !.
simp←time(1,Y,Y) :- !.
simp←time(X,Y,X*Y).
simp←expt(←,0,1) :- !.
simp←expt(X,1,X) :- !.
simp←expt(X,N,X↑N).
diff1(DF) :- d(x↑3+3*x↑2+3*x+1,x,DG),d(DG,x,DF).
diff2(DF) :-
d((x-1)↑5,x,D1),d(D1,x,D2),d(D2,x,D3),d(D3,x,D4),d(D4,x,DF).
/*
[15-1:] D(x↑3+3*x↑2+3*x+1)/DX
do "q151(100)." for one hundred iterations.
*/
q151(N) :-
statistics(garbage←collection,[←,←|G1]),!,
statistics(runtime,[←,←]),!,
loop←q151(0,N),
statistics(runtime,[←,T1]),!,
statistics(garbage←collection,[←,←|G2]),!,
statistics(runtime,[←,←]),!,
loop←dummy(0,N),
statistics(runtime,[←,T2]),
statistics(garbage←collection,[←,←|G3]),!,
G1 = [Gt1], G2 = [Gt2], G3 = [Gt3],
G4 is Gt2 + Gt2 - Gt1 - Gt3,
T3 is T1-T2-G4, Total is T1-T2,
write('Total = '), write(Total),
write('ms, runtime = '), write(T3),
write('ms, gctime = '), write(G4),
write('ms, for '), write(N), write(' iterations.'), nl.
loop←q151(N,N) :- !.
loop←q151(I,N) :-
I1 is I+1, diff1(DF), !, loop←q151(I1,N).
loop←dummy(N,N) :- !.
loop←dummy(I,N) :-
I1 is I+1, !, loop←dummy(I1,N).
/*
[15-2:] D↑5((x-1)↑5)/DX
do "q152(1)." for only once.
*/
q152(N) :-
statistics(garbage←collection,[←,←|G1]),!,
statistics(runtime,[←,←]),!,
loop←q152(0,N),
statistics(runtime,[←,T1]),!,
statistics(garbage←collection,[←,←|G2]),!,
statistics(runtime,[←,←]),!,
loop←dummy(0,N),
statistics(runtime,[←,T2]),
statistics(garbage←collection,[←,←|G3]),!,
G1 = [Gt1], G2 = [Gt2], G3 = [Gt3],
G4 is Gt2 + Gt2 - Gt1 - Gt3,
T3 is T1-T2-G4, Total is T1-T2,
write('Total = '), write(Total),
write('ms, runtime = '), write(T3),
write('ms, gctime = '), write(G4),
write('ms, for '), write(N), write(' iterations.'), nl.
loop←q152(N,N) :- !.
loop←q152(I,N) :-
I1 is I+1, diff2(DF), !, loop←q152(I1,N).